package com.yuan.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.Mapper.EmployeeMapper;
import com.yuan.Service.EmployeeService;
import com.yuan.constant.MessageConstant;
import com.yuan.constant.PasswordConstant;
import com.yuan.constant.StatusConstant;
import com.yuan.context.BaseContext;
import com.yuan.dto.EmployeeDTO;
import com.yuan.dto.EmployeeLoginDTO;
import com.yuan.dto.EmployeePageQueryDTO;
import com.yuan.entity.Employee;
import com.yuan.exception.AccountLockedException;
import com.yuan.exception.AccountNotFoundException;
import com.yuan.exception.PasswordEditFailedException;
import com.yuan.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Override
    public void saveSelf(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        BeanUtils.copyProperties(employeeDTO, employee);

        employee.setStatus(StatusConstant.ENABLE);
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));


        //设置当前记录的创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());

        save(employee);

    }

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        wrapper.eq(Employee::getUsername, employeeLoginDTO.getUsername());

        Employee employee = getOne(wrapper);

        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        String pass = DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes());

        if (!pass.equals(employee.getPassword())) {
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_ERROR);
        }
        if (employee.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;
    }

    @Override
    public PageResult pageSelf(EmployeePageQueryDTO dto) {

        int page = dto.getPage();
        int pageSize = dto.getPageSize();
        String name = dto.getName();
        Page<Employee> page1 = new Page(page, pageSize);
        if (name != null && name != "") {
            LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employee::getName, name);
            Page<Employee> paged = page(page1, wrapper);
            PageResult pageResult = new PageResult();
            pageResult.setTotal(paged.getTotal());
            pageResult.setRecords(paged.getRecords());
            return pageResult;
        }

        Page<Employee> paged = page(page1);
        PageResult pageResult = new PageResult(paged.getTotal(), paged.getRecords());

        return pageResult;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = getById(id);

        employee.setStatus(status);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());

        updateById(employee);


    }


}
